﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using NHibernate;

namespace Data
{
    public class CustomerRespository:ICustomerRespository
    {
        private readonly ISession _session = NhibernateHelper.CurrentSessionFactory.OpenSession();
        public void Insert(Customer customer)
        {
            int id = (int)_session.Save(customer);
            _session.Flush();
            return;
        }

        public void Update(Customer customer)
        {
            using (ITransaction ts = _session.BeginTransaction())
            {
                try
                {
                    _session.Update(customer);
                    _session.Flush();
                    ts.Commit();
                }
                catch (Exception)
                {
                    ts.Rollback();
                    throw;
                }
            }
        }

        public void Delete(Customer customer)
        {
            _session.Delete(customer);
            _session.Flush();
        }

        public IList<Customer> Query()
        {
            IList<Customer> list = _session.QueryOver<Customer>().OrderBy(c=>c.CreateTime).Desc.List();
            return list;
        }
    }
}
